package com.talkology.pilot.manager.controller;

import com.talkology.pilot.manager.service.impl.FileMgtServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@RestController
@RequestMapping(value = "/file")
public class FileMgtController {
    @Autowired
    private FileMgtServiceImpl service;

    @ApiOperation(value = "设置老师头像", notes = "返回存储的文件地址，用于添加老师时添加image地址; 对于已经存在的老师也可以直接更换图片即可，这个接口会自动更新库。")
    @PutMapping("/profiles")
    @ResponseBody
    public String setUserProfile(@RequestParam(value = "teacherIndex", required = true) Long teacherIndex,
                                 @RequestParam(required = true) MultipartFile profile) {
        return service.updTeacherProfile(teacherIndex, profile);
    }

    @ApiOperation(value = "根据文件名获取图片文件", notes = "http(s)获取头像")
    @RequestMapping(value = "/image", produces = MediaType.IMAGE_JPEG_VALUE, method = RequestMethod.GET)
    @ResponseBody
    public byte[] getImage(@RequestParam(value = "imageName", required = true) String imageName) throws IOException {
        File file = new File(imageName);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        return bytes;
    }

}
